package servlet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import src.util.Message;
import src.util.MessageCfg;
import util.RomWhiteList;

public class RomTranslatorServlet extends NetPlayServlet{

	private Logger log = Logger.getLogger(RomTranslatorServlet.class);
	
	public RomTranslatorServlet()
	{
		
	}
		
	public void romTranslator()
	{
		//1 - getting request rom name
		String romName = getReq().getParameter("romTags");
		log.warn("input = " + romName);
		
		//2 - spliting rom Name with the following chars . _ - *espace*
		String[] nameParts = romName.split("[,._-]");
		String gameKey = "unknown";
		
		for(String part : nameParts)
		{
			gameKey = findValueInMap(part);	
			if(!gameKey.equals("unknown"))
				break;
		}
		
		log.warn("gamekey = " + gameKey);
		
		new Message( new MessageCfg( true, true));
		Message message = new Message( );
		
		send( message.toClient( 200, gameKey));
	}
	
	
	public String findValueInMap(String part)
	{
		for (Entry<String, List<String>> currentEntry : RomWhiteList.getEntrySet()) {
	        String id = currentEntry.getKey();
	        List<String> values = currentEntry.getValue();
	        if(values.contains(part))
	        {
	        	return id;
	        }
		}
		
		return "unknown";
	}
	
}
